package com.example.walletdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.walletdemo.common.utils.ResponseResult;
import com.example.walletdemo.entity.pojo.UserWallet;
import com.example.walletdemo.entity.pojo.WalletRecord;
import com.example.walletdemo.entity.vo.TransactionVo;
import com.example.walletdemo.enums.AppHttpCodeEnum;
import com.example.walletdemo.mapper.UserWalletMapper;
import com.example.walletdemo.mapper.WalletRecordMapper;
import com.example.walletdemo.service.UserWalletService;
import com.example.walletdemo.service.WalletRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: 包
 * @Date: 2023/2/20 22:57
 * Description:
 */
@Service
public class WalletRecordServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements WalletRecordService {

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Autowired
    private WalletRecordMapper walletRecordMapper;

    /**
     * 用户消费100元
     * @param transactionVo
     * @return
     */
    @Override
    public ResponseResult consume(TransactionVo transactionVo) {
        QueryWrapper<UserWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("wallet_id", transactionVo.getWalletId())
                .eq("user_id", transactionVo.getUserId());
        UserWallet userWallet = userWalletMapper.selectOne(queryWrapper);
        if (userWallet == null){
            return ResponseResult.errorResult(203,"参数错误,该账户不存在");
        }else {
            BigDecimal balance = userWallet.getBalance();
            BigDecimal temp = balance.subtract(transactionVo.getAmount());
            if (temp.compareTo(BigDecimal.ZERO)<0){
                return ResponseResult.errorResult(201,"余额不足");
            }else {
                //修改钱包余额
                userWallet.setBalance(temp);
                userWallet.setUpdateTime(new Date());
                userWalletMapper.updateById(userWallet);

                //添加新的交易记录
                WalletRecord walletRecord = new WalletRecord();
                walletRecord.setWalletId(transactionVo.getWalletId());
                walletRecord.setOrderId(transactionVo.getOrderId());
                walletRecord.setAmount(transactionVo.getAmount());
                walletRecord.setBalance(balance);
                walletRecord.setRecordType(transactionVo.getRecordType());
                walletRecord.setCreateTime(new Date());
                walletRecordMapper.insert(walletRecord);

                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }

    }

    /**
     * 用户退款20元
     * @param transactionVo
     * @return
     */
    @Override
    public ResponseResult refund(TransactionVo transactionVo) {
        QueryWrapper<UserWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("wallet_id", transactionVo.getWalletId())
                .eq("user_id", transactionVo.getUserId());
        UserWallet userWallet = userWalletMapper.selectOne(queryWrapper);
        if (userWallet == null){
            return ResponseResult.errorResult(203,"参数错误,该账户不存在");
        }else {
            BigDecimal balance = userWallet.getBalance();
            //修改钱包余额
            userWallet.setBalance(balance.add(transactionVo.getAmount()));
            userWallet.setUpdateTime(new Date());
            userWalletMapper.updateById(userWallet);

            //添加新的交易记录
            WalletRecord walletRecord = new WalletRecord();
            walletRecord.setWalletId(transactionVo.getWalletId());
            walletRecord.setOrderId(transactionVo.getOrderId());
            walletRecord.setAmount(transactionVo.getAmount());
            walletRecord.setBalance(transactionVo.getAmount());
            walletRecord.setRecordType(transactionVo.getRecordType());
            walletRecord.setCreateTime(new Date());
            walletRecordMapper.insert(walletRecord);

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }

    /**
     * 查询用户钱包金额变动明细
     * @param walletId
     * @return
     */
    @Override
    public ResponseResult recordDetails(Long walletId) {
        List<WalletRecord> res = walletRecordMapper.selectList(new QueryWrapper<WalletRecord>().eq("wallet_id",walletId));
        return ResponseResult.okResult(res);
    }


}
